﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kinect;
using System.Timers;
using System.Windows.Threading;
using Vector = Microsoft.Research.Kinect.Nui.Vector;

using Matrix = Microsoft.Xna.Framework.Matrix;
using Vector3 = Microsoft.Xna.Framework.Vector3;

namespace PresatationProject
{
    /// <summary>
    /// TestPage.xaml 的交互逻辑
    /// </summary>
    public partial class TestPage : Page
    {
        private KinectController _kinect;
        public TestPage()
        {
            _kinect = new KinectController(false);
            InitializeComponent();
           // _kinect.outHand += new KinectController.outGetHandPositionEventHandler(HandPosition);
            person.Items.Add(_kinect.ViewSkeleton);
            //Timer count= new Timer(10);
            //count.Elapsed +=new ElapsedEventHandler(count_Elapsed);
            //count.Start();
            Window_ContentRendered();
        }

        DispatcherTimer 帧计时器;

        private void Window_ContentRendered()
        {
            帧计时器 = new DispatcherTimer();
            帧计时器.Tick += new EventHandler(帧计时器_Tick);
            帧计时器.Interval = TimeSpan.FromMilliseconds(50);
            帧计时器.Start();
        }

        void 帧计时器_Tick(object sender, EventArgs e)
        {

            if (GHands.Count < 3)
            {
                var p = System.Windows.Forms.Control.MousePosition;
                handmouse.beta_MouseMove(p.X, p.Y);
                //x = (float)p.X;
                //y = (float)p.Y;
            }
            else
            {
                handmouse.flag = true;
                GPoint p = refixPoint(_kinect.GHand);
                x = (float)p.x /** 1024*/;
                y = (float)p.y /** 768*/;
                handmouse.beta_MouseMove(x, y);
//                handxp.Text = x + " " + y;
            }
            //do
            //{
            //    GPoint p = _kinect.GHand;
            //    x = (float)p.x * 1024;
            //    y = (float)p.y * 768;
            //    handmouse.beta_MouseMove(x, y);
            //    handxp.Text = x + " " + y;
            //} while (false);
        }

        float x = -1;
        float y = -1;
        //double alpha, beta;

        double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
        double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;        

        private void count_Elapsed(object sender,ElapsedEventArgs e)
        {
            x = _kinect.Handx*1024;
            y = _kinect.Handy*768;
            //handxp.Text = x +"  "+ y;
            //handmouse.Beta_MouseMove(x,y);
        }
        private long now = System.Environment.TickCount;
        public void HandPosition(object sender, MouseEvent e)
        {
            //MessageBox.Show("OK?");
            //handmouse.test_MouseMove(sender, e);
            handxp.Text = e.x + "  " + e.y;
        }

        List<Kinect.GPoint> GHands = new List<GPoint>();
        List<Kinect.GPoint> GScreens = new List<GPoint>();

        //List<Vector> GHands = new List<Vector>();
        //List<System.Drawing.Point> GScreens = new List<System.Drawing.Point>();

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (GHands.Count >= 3) return;

            GHands.Add(_kinect.GHand);
            GScreens.Add( new GPoint(System.Windows.Forms.Control.MousePosition) );
            txtTmp.Text = GHands.Count.ToString();
//            System.Windows.MessageBox.Show("Orz........");            
        }

        private GPoint refixPoint(GPoint P)
        {
            //GPoint ret = new GPoint();
            Matrix M = new Matrix();
            M.M11 = (float)(GHands[1].x - GHands[0].x); M.M12 = (float)(GHands[2].x - GHands[0].x);
            M.M21 = (float)(GHands[1].y - GHands[0].y); M.M22 = (float)(GHands[2].y - GHands[0].y);
            //M.M33 = 1;
            //M.M44 = 1;
            //M = Matrix.Invert(M);
            double D = M.M11 * M.M22 - M.M12 * M.M21;
            Vector3 v = new Vector3();
            v.X = (float)(P.x - GHands[0].x); v.Y = (float)(P.y - GHands[0].y);
            double D1 = v.X * M.M22 - v.Y * M.M12;
            double D2 = M.M11 * v.Y - M.M21 * v.X;
//            v = Vector3.Transform(v, M);
            v.X = (float)(D1 / D);
            v.Y = (float)(D2 / D);
            handxp.Text = (v.X + "  " + v.Y).ToString();
            GPoint ret = GPoint.add(GScreens[1].sub(GScreens[0]).mul(v.X), GScreens[2].sub(GScreens[0]).mul(v.Y)).add(GScreens[0]);
            return ret;
        }

        public void GG()
        {
            //            base.KeyDown(e);
            if (GHands.Count >= 3) return;

            GHands.Add(_kinect.GHand);
            GScreens.Add(new
            GPoint(System.Windows.Forms.Control.MousePosition));
            txtTmp.Text = GHands.Count.ToString();

            if (GHands.Count == 3)
            {
                Kinect.GFileWriter writer = new Kinect.GFileWriter();
                writer.open("e:/cxBadGuy.txt");
                for (int i = 0; i < GHands.Count; i++)
                    writer.writeln(GHands[i]);
                for (int i = 0; i < GScreens.Count; i++)
                    writer.writeln(GScreens[i]);
                writer.close();
            }

            //            System.Windows.MessageBox.Show("Orz........");
        }
        
    }
}
